$(function(){
	// 初始化验证控件
	initFormValidator();
});



/**
 * 初始化表单验证
 */
function initFormValidator(){
	  $('#saveForm').bootstrapValidator({
        message: '输入项验证失败',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',		// 错误
            invalid: 'glyphicon glyphicon-remove',	// 正确
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
        	"condition.name": {
            	validators: {
            		notEmpty: {message: '请输入用户名!'},
            		remote: {
                         url: basePath+"member/checkMemberByCondition.htm",
                         type : "post",
                         dataType: "json",           //接受数据格式   
                         data: {                     //要传递的数据
	                          "condition.name":$("#account").val()
                         },
                         message : "账户已存在，请重新输入!"
                     }
            	}
            },
            "condition.invokeCode": {
            	validators: {
            		notEmpty: {message: '请输入邀请人!'}
            	}
            },
            "condition.twoPassword": {
            	validators: {
            		notEmpty: {message: '请输入二级密码!'}
            	}
            },
            "condition.password": {
            	validators: {
            		notEmpty: {message: '请输入密码!'}
            	}
            },
        }
	  }).on('success.form.bv', function(e) {
		  // 阻止默认事件提交
          e.preventDefault();
    	  submitMemberFormFN();
          return false;
      });  
}

/**
 * 提交表单
 * @returns {Boolean}
 */
function submitMemberFormFN(){
	 // 获取form实例
    var $form = $("#saveForm");
    var params = $form.serializeArray();
    $.post(basePath+"member/save.htm",params,
  		  function(data){
  	  			if(data.status == "success"){
  	  				toastr.success("保存成功！");
  	  				setTimeout(function(){
  	  					window.location.href=basePath+"member/index.htm";
  	  				},timeOut);
  	  			}else{
  	  				toastr.error("保存失败："+data.message);
  	  			}
    		  }
    ,"json");
    return false;
}